import logging
import wx

import pyciv.hexagon
import pyciv.resources

class Layer():
    def render(self, surface, hexes):
        pass

class Outline(Layer):
    def render(self, surface, hexes, layouts):
        for hex in hexes:
            layout = layouts[hex]
            surface.DrawPolygon(layout.points)

class Terrain(Layer):
    def __init__(self, state):
        self._brush = wx.Brush(wx.BLACK)   
        self._pen = wx.Pen(wx.BLACK, 2)
        self._state = state

    def render(self, surface, hexes, layouts):
        for hex in hexes:
            layout = layouts[hex]
            if self._state.intel[hex] == 0:
                self._brush.SetColour(self._state.terrain[hex].color[0])
            else:
                self._brush.SetColour(self._state.terrain[hex].color[1])
            surface.SetBrush(self._brush)
            if self._state.victory[hex] > 0:
                self._pen.SetColour(wx.Colour(255, 100, 100))
            else:
                if self._state.intel[hex] == 0:
                    self._pen.SetColour(wx.BLACK)
                else:
                    self._pen.SetColour(wx.Colour(50,50,50))
            surface.SetPen(self._pen)
            surface.DrawPolygon(layout.points)
            
                

class Coordinate(Layer):
    def __init__(self):
        pass

    def render(self, surface, hexes, layouts):
        for hex in hexes:
            layout = layouts[hex]
            surface.DrawText(str(hex), 
                             layout.points[4][0], 
                             layout.points[4][1])

class Owner(Layer):
    def __init__(self, state):
        self._state = state
        self._pen = wx.Pen(wx.BLACK, 3)
        self._font = wx.Font(pointSize=8, 
                             family=wx.FONTFAMILY_DEFAULT,
                             style=wx.FONTSTYLE_NORMAL,
                             weight=wx.BOLD)

    def _draw_text(self, surface, center, text):
        (width, height) = surface.GetTextExtent(text)
        surface.SetFont(self._font)
        surface.DrawText(text,
                         center[0] - (width / 2),
                         center[1] - height)
        return height

    def render(self, surface, hexes, layouts):
        for hex in hexes:
            layout = layouts[hex]
            index = 0
            if not self._state.owner[hex].is_default_player():
                if self._state.intel[hex] > 0:
                    for neighbour in pyciv.hexagon.get_neighbours(hex):
                        if self._state.owner.has_key(neighbour):
                            if self._state.owner[hex] != self._state.owner[neighbour]:
                                self._pen.SetColour(self._state.owner[hex].color)
                                surface.SetPen(self._pen)
                                surface.DrawLine(layout.points[index][0], 
                                                 layout.points[index][1], 
                                                 layout.points[(index + 1) % 6][0],
                                                 layout.points[(index + 1) % 6][1])
                        index += 1
            surface.SetTextForeground(wx.BLACK)
            if self._state.supply[hex] > 0:
                height = self._draw_text(surface,
                                         layout.center,
                                         "+%d SP"  % (self._state.supply[hex]))

class Unit(Layer):
    def __init__(self, state):
        self._state = state
        self._font = wx.Font(pointSize=8, 
                             family=wx.FONTFAMILY_DEFAULT,
                             style=wx.FONTSTYLE_NORMAL,
                             weight=wx.BOLD)
        self._brush = wx.Brush(wx.BLACK)
        self._pen = wx.Pen(wx.BLACK, 2)

    def _draw_text(self, surface, center, text):
        (width, height) = surface.GetTextExtent(text)
        surface.SetFont(self._font)
        surface.DrawText(text,
                         center[0] - (width / 2),
                         center[1] - height)
        return height

    def render(self, surface, hexes, layouts):
        for hex in hexes:
            if self._state.unit[hex] is not pyciv.unit.NONE and self._state.intel[hex] > 0:
                layout = layouts[hex]
                self._brush.SetColour(self._state.owner[hex].color)
                surface.SetBrush(self._brush)
                surface.SetPen(self._pen)
                surface.DrawCircle(layout.center[0], layout.center[1], 22)
                if self._state.unit[hex].ready_for_action():
                    surface.SetTextForeground(wx.BLACK)
                else:
                    surface.SetTextForeground(wx.LIGHT_GREY)
                height = self._draw_text(surface,
                                         layout.center,
                                         "%d, %d"  % (self._state.unit[hex].strength(), 
                                                      self._state.unit[hex].attributes[pyciv.attribute.ACTIONPOINTS]))

class Selected(Layer):
    def __init__(self, engine):
        self._engine = engine
        self._pen = wx.Pen(wx.LIGHT_GREY, 4)
        self._font = wx.Font(pointSize=9, 
                             family=wx.FONTFAMILY_DEFAULT,
                             style=wx.FONTSTYLE_NORMAL,
                             weight=wx.NORMAL)
                             
    def _draw_text(self, surface, center, text):
        (width, height) = surface.GetTextExtent(text)
        surface.SetFont(self._font)
        surface.DrawText(text,
                         center[0] - (width / 2),
                         center[1] - height)
        return height

    def render(self, surface, hexes, layouts):
        for hex in hexes:
            layout = layouts[hex]

            if self._engine.current_hex == hex:
                surface.SetPen(self._pen)
                for index in range(0, 6):
                    surface.DrawLine(layout.points[index][0], 
                                     layout.points[index][1], 
                                     layout.points[(index + 1) % 6][0],
                                     layout.points[(index + 1) % 6][1])
            if self._engine.hex_action:
                if self._engine.hex_action[hex]:
                    name = self._engine.hex_action[hex].name
                    if self._engine.hex_action[hex].enabled:
                        surface.SetTextForeground(wx.LIGHT_GREY)
                    else:
                        surface.SetTextForeground(wx.BLACK)
                    height = surface.GetCharHeight()
                    self._draw_text(surface,
                                    (layout.center[0], layout.center[1] + height),
                                    name) 
